package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotCompanyUser;
import com.ruoyi.wisdom.domain.swap.SwapCompanyUser;
import com.ruoyi.wisdom.service.IIotCompanyUserService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CompanyUserEvent extends IotEvent{

    @Autowired
    private IIotCompanyUserService iotCompanyUserService;


    /**
     * 数据处理
     *
     * @param companyUser
     */
    public void handle(SwapCompanyUser companyUser) {
        if (companyUser.getOperate() == null) {
            save(companyUser);
        } else {
            switch (companyUser.getOperate()) {
                case "delete":
                case "remove":
                    delete(companyUser);
                    break;
                default:
                    save(companyUser);
            }
        }
    }

    /**
     * 添加|更新
     *
     * @param swapCompanyUser
     */
    public void save(SwapCompanyUser swapCompanyUser) {
        IotCompanyUser params = new IotCompanyUser();
        params.setUid(swapCompanyUser.getUid());
        IotCompanyUser result = iotCompanyUserService.selectIotCompanyUser(params);
        // 实体装换
        IotCompanyUser companyUser = getModelMapper().map(swapCompanyUser, IotCompanyUser.class);
        if (result != null) {
            companyUser.setId(result.getId());
            iotCompanyUserService.updateIotCompanyUser(companyUser);
        } else {
            iotCompanyUserService.insertIotCompanyUser(companyUser);
        }
    }

    /**
     * 删除
     *
     * @param swapCompanyUser
     */
    public void delete(SwapCompanyUser swapCompanyUser) {
        IotCompanyUser params = new IotCompanyUser();
        params.setUid(swapCompanyUser.getUid());
        IotCompanyUser result = iotCompanyUserService.selectIotCompanyUser(params);
        if (result != null) {
            iotCompanyUserService.deleteIotCompanyUserById(result.getId());
        }
    }
}
